#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "lwip/sockets.h"
#include "wifi_sta_connect.h"
#include "ws2812_module.h"


#define SELECT_WIFI_SSID "NXROBO_TEST"
#define SELECT_WIFI_PASSWORD "rightright"
#define SELECT_WIFI_IPADDRESS "192.168.3.248"
#define SELECT_WIFI_GATEWAY "192.168.3.1"
#define _PROT_ 10006
#define TCP_BACKLOG 10
char *default_wifi = "set_wifi:{\"wifi_master\":{\"ssid\":\"NXROBO_TEST\",\"password\":\"rightright\",\"ipaddress\":\"192.168.3.248\",\"gateway\":\"192.168.3.1\"}}";

//在sock_fd 进行监听，在 new_fd 接收新的链接
int sock_fd, new_fd;
char recvbuf[128];
char sendbuf[128];

char *buf = "Hello! I'm SPARK-HM-PI TCP Server!";

extern int keep_going;
extern int new_status;
void Recv_Msg_Handle(char *recvbuf)
{
	if(new_status!=2)
	{
		new_status = 2;
		keep_going = 0;
	}

	if(!strcmp("open",recvbuf))
	{
		new_status = 3; //新状态为打开虚拟门。
		keep_going = 0;
		printf("receive open!\n");
	}
	else if(!strcmp("close",recvbuf))
	{
		new_status = 4; //新状态为已连接网络。
		keep_going = 0;
		printf("receive close!\n");
	}
	else
	{
		printf("other!\n");
	}
}

 void TCPServerTask(void)
{
	//服务端地址信息
	struct sockaddr_in server_sock;

	//客户端地址信息
	struct sockaddr_in client_sock;
	int sin_size;

	struct sockaddr_in cli_addr;
	if(WifiConnect(SELECT_WIFI_SSID, SELECT_WIFI_PASSWORD , SELECT_WIFI_IPADDRESS, SELECT_WIFI_GATEWAY)==-1)
		return;

	//创建socket
	if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		perror("socket is error\r\n");
		exit(1);
	}

	bzero(&server_sock, sizeof(server_sock));
	server_sock.sin_family = AF_INET;
	server_sock.sin_addr.s_addr = htonl(INADDR_ANY);
	server_sock.sin_port = htons(_PROT_);

	//调用bind函数绑定socket和地址
	if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1)
	{
		perror("bind is error\r\n");
		exit(1);
	}

	//调用listen函数监听(指定port监听)
	if (listen(sock_fd, TCP_BACKLOG) == -1)
	{
		perror("listen is error\r\n");
		exit(1);
	}
	new_status = 1;
	keep_going = 0;
	printf("start accept\n");
	//调用accept函数从队列中
	while (1)
	{
		sin_size = sizeof(struct sockaddr_in);

		if ((new_fd = accept(sock_fd, (struct sockaddr *)&client_sock, (socklen_t *)&sin_size)) == -1)
		{
			perror("accept");
			continue;
		}

		// cli_addr = malloc(sizeof(struct sockaddr));

		printf("accept addr\r\n");

		memcpy(&cli_addr, &client_sock, sizeof(struct sockaddr));


		//处理目标
		ssize_t ret;

		while (1)
		{
			//连接断开防止跳不出循环
			if ((ret = recv(new_fd, recvbuf, sizeof(recvbuf), 0)) <= 0)
			{
				printf("recv error \r\n");

				break;
			}
			printf("recv :%s\r\n", recvbuf);
			// printf("%d %d %d\r\n",strlen(recvbuf),strcmp(recvbuf,"-1"),strcmp("5",recvbuf));
			//处理收到的信息
			Recv_Msg_Handle(recvbuf);
			bzero(recvbuf,sizeof(recvbuf));

		}
		closesocket(new_fd);
	}
}


char recv_msg[200];
char first_time = 1;


void SparkCmdSlaverEntry(void)
{
	//初始化
	osThreadAttr_t attr;

	attr.name = "TCPServerTask";
	attr.attr_bits = 0U;
	attr.cb_mem = NULL;
	attr.cb_size = 0U;
	attr.stack_mem = NULL;
	attr.stack_size = 10240;
	attr.priority = osPriorityNormal;

	if (osThreadNew((osThreadFunc_t)TCPServerTask, NULL, &attr) == NULL)
	{
		printf("[TCPServerDemo] Falied to create TCPServerTask!\n");
	}


	attr.name = "WS2812Task";
	attr.stack_size = 1024*4;
	if (osThreadNew((osThreadFunc_t)WS2812Task, NULL, &attr) == NULL)
	{
		printf("[WS2812Task] Falied to create WS2812Task!\n");
	}


}

APP_FEATURE_INIT(SparkCmdSlaverEntry);
